﻿namespace MicroCloud.Extensions
{
    public static partial class IServiceCollectionExtensions
    {
        #region ServiceLog
        /// <summary>
        /// 添加服务调试级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="serviceType">服务类型</param>
        /// <param name="implementType">服务实例类型</param>
        /// <param name="logType">日志类型</param>
        /// <param name="lifetime">服务生命周期</param>
        /// <returns></returns>
        public static IServiceCollection ServiceLogDebug(this IServiceCollection services, Type serviceType, Type implementType, Type logType, ServiceLifetime lifetime = ServiceLifetime.Singleton)
        {
            string lifetimeType = lifetime == ServiceLifetime.Singleton ? "单例" : lifetime == ServiceLifetime.Scoped ? "作用域" : "瞬时";
            return services.LogDebug($"添加 {lifetimeType} 服务 {serviceType.FullName}[{implementType.FullName}]", logType);
        }

        /// <summary>
        /// 添加服务调试级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="oldDescriptors">服务描述器</param>
        /// <param name="logName">日志名称</param>
        /// <returns></returns>
        public static IServiceCollection ServiceLogDebug(this IServiceCollection services, ServiceDescriptor[] oldDescriptors, string logName)
        {
            var list = services.Except(oldDescriptors);
            foreach (ServiceDescriptor desc in list)
            {
                if (desc.ImplementationType != null)
                {
                    services.ServiceLogDebug(desc.ServiceType, desc.ImplementationType, logName, desc.Lifetime);
                    continue;
                }

                if (desc.ImplementationInstance != null)
                {
                    services.ServiceLogDebug(desc.ServiceType, desc.ImplementationInstance.GetType(), logName, desc.Lifetime);
                }
            }

            return services;
        }

        /// <summary>
        /// 添加服务调试级别日志
        /// </summary>
        /// <typeparam name="TServiceType">服务类型</typeparam>
        /// <typeparam name="TImplementType">服务实例类型</typeparam>
        /// <param name="services">服务集合</param>
        /// <param name="logName">日志名称</param>
        /// <param name="lifetime">服务生命周期</param>
        /// <returns></returns>
        public static IServiceCollection ServiceLogDebug<TServiceType, TImplementType>(this IServiceCollection services, string logName, ServiceLifetime lifetime = ServiceLifetime.Singleton)
        {
            Type serviceType = typeof(TServiceType), implementType = typeof(TImplementType);
            return services.ServiceLogDebug(serviceType, implementType, logName, lifetime);
        }

        /// <summary>
        /// 添加服务调试级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="serviceType">服务类型</param>
        /// <param name="implementType">服务实例类型</param>
        /// <param name="logName">日志名称</param>
        /// <param name="lifetime">服务生命周期</param>
        /// <returns></returns>
        public static IServiceCollection ServiceLogDebug(this IServiceCollection services, Type serviceType, Type implementType, string logName, ServiceLifetime lifetime = ServiceLifetime.Singleton)
        {
            string lifetimeType = lifetime == ServiceLifetime.Singleton ? "单例" : lifetime == ServiceLifetime.Scoped ? "作用域" : "瞬时";
            return services.LogDebug($"添加 {lifetimeType} 服务 {serviceType.FullName}[{implementType.FullName}]", logName);
        }

        #endregion

        #region StartupLogger
        /// <summary>
        /// 添加启动调试级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="message">消息</param>
        /// <param name="logType">日志类型</param>
        /// <returns></returns>
        public static IServiceCollection LogDebug(this IServiceCollection services, string message, Type logType)
        {
            StartupLogger logger = services.GetOrAddSingleton(() => new StartupLogger());
            logger.LogDebug(message, logType);
            return services;
        }

        /// <summary>
        /// 添加启动信息级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="message">消息</param>
        /// <param name="logType">日志类型</param>
        /// <returns></returns>
        public static IServiceCollection LogInformation(this IServiceCollection services, string message, Type logType)
        {
            StartupLogger logger = services.GetOrAddSingleton(() => new StartupLogger());
            logger.LogInformation(message, logType);
            return services;
        }

        /// <summary>
        /// 添加启动调试级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="message">消息</param>
        /// <param name="logName">日志名称</param>
        /// <returns></returns>
        public static IServiceCollection LogDebug(this IServiceCollection services, string message, string logName)
        {
            StartupLogger logger = services.GetOrAddSingleton(() => new StartupLogger());
            logger.LogDebug(message, logName);
            return services;
        }

        /// <summary>
        /// 添加启动信息级别日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="message">消息</param>
        /// <param name="logName">日志名称</param>
        /// <returns></returns>
        public static IServiceCollection LogInformation(this IServiceCollection services, string message, string logName)
        {
            StartupLogger logger = services.GetOrAddSingleton(() => new StartupLogger());
            logger.LogInformation(message, logName);
            return services;
        }

        #endregion
    }
}
